package com.smf.lsc;

import java.io.UnsupportedEncodingException;
import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smf.lsc.authorization.security.properties.OAuth2Properties;
import com.smf.lsc.authorization.security.util.JsonUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 测试controller
 * Created on 2021/11/19.
 *
 * @author ln
 * @since 1.0
 */
@Slf4j
@RestController
public class DemoController {

	@Autowired
	private OAuth2Properties oAuth2Properties;

	@GetMapping("/userJwt")
	public Object getCurrentUserJwt(Authentication authentication, HttpServletRequest request)
			throws UnsupportedEncodingException {
		SecurityContext ss = SecurityContextHolder.getContext();
		log.info("【SecurityOauth2Application】 getCurrentUserJwt authentication={}", JsonUtil.toJson(authentication));

		String header = request.getHeader("Authorization");
		String token = StringUtils.substringAfter(header, "bearer ");

//		Claims claims = Jwts.parser().setSigningKey(oAuth2Properties.getJwtSigningKey().getBytes("UTF-8"))
//				.parseClaimsJws(token).getBody();
//		String blog = (String) claims.get("blog");
//		log.info("【SecurityOauth2Application】 getCurrentUser1 blog={}", blog);

		return authentication;
	}

	@GetMapping("/userRedis")
	public Object getCurrentUserRedis(Authentication authentication) {
		log.info("【SecurityOauth2Application】 getCurrentUserRedis authentication={}", JsonUtil.toJson(authentication));

		return authentication;
	}

	@GetMapping("/user/me")
	public Principal user(Principal user) {
		return user;
	}
	
	@GetMapping("/captchaCode/get")
	public String captchaCode() {
		return "captchaCode";
	}
	
}
